export default {
    computed: {
        user() {
            return this.$store.state.user.data;
        },
    },
    data() {
        const validateRoleName = (rule, value, callback) => {
            if (value === '' || value.replace(/(^\s*)|(\s*$)/g, "") === "") {
                callback(new Error('不能为空,请正确填写'));
            } else {
                callback();
            }
        }
        const validatePassword = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('密码不能为空'));
                return
            }
            if (value.length < 6) {
                callback(new Error('请输入至少6位数密码'));
                return
            }
            callback();
        }
        const validateSelect = (rule, value, callback) => {
            if (value === '' || value.replace(/(^\s*)|(\s*$)/g, "") === "") {
                callback(new Error('请为新用户分配初始权限'));
            } else {
                callback();
            }
        }
        const validateTel = (rule, value, callback) => {

            if (value && !(/^1[34578]\d{9}$/).test(value)) {
                callback(new Error('请输入正确的手机号'));
            } else {
                callback();
            }
        }
        return {
            personalInfoFormRules: {
                name: [{
                    required: true,
                    validator: validateRoleName,
                    trigger: 'blur'
                }],
                mail: [{
                    required: false,
                    type: 'email',
                    message: '请输入正确的email',
                    trigger: 'blur'
                }],
                tel: [{
                    required: false,
                    validator: validateTel,
                    trigger: 'blur'

                }],

            },
            personalInfoForm: {
                name: '',
                password: '',
                roles: '',
                mail: '',
                tel: '',
                oldpassword: ''
            },
            passwordForm: {
                old: '',
                new: ''
            },
            passwordFormRules: {
                old: [{
                    required: true,
                    validator: validatePassword,
                    trigger: 'blur'

                }],
                new: [{
                    required: true,
                    validator: validatePassword,
                    trigger: 'blur'

                }],
            },
            isLoading: false,
            pswIsLoading: false,
            uploadImgdata: {},
            imgUploadPath: '',
            isUploading: false,
            uploadProgress: 0
        }
    },
    methods: {
        submitChange() {
            this.isLoading = true;
            if (this.personalInfoForm.mail == this.user.email && this.personalInfoForm.tel === this.user.tel) {
                this.$Notice.warning({
                    title: '尚未作出修改',
                });
                this.isLoading = false;
                return
            }
            this.http.post(this.postUrl.updateUserInfo, {
                token: this.user.tokenList[0],
                tel: this.personalInfoForm.tel,
                email: this.personalInfoForm.mail
            }).then((data) => {
                this.isLoading = false;
                if (data.code > -1) {
                    this.user.email = this.personalInfoForm.mail;
                    this.user.tel = this.personalInfoForm.tel;
                    this.$Notice.success({
                        title: '修改资料成功！',
                    });
                    this.$store.commit('updateUser', this.user, true);
                } else {
                    this.errorHandle(data.msg)

                }
            }, () => {
                this.errorHandle()
                this.isLoading = false;
            })
        },
        submitChangePassword() {
            this.$refs.passwordForm.validate((valid) => {
                if (valid) {
                    this.pswIsLoading = true;
                    this.http.post(this.postUrl.changePassword, {
                        token: this.user.tokenList[0],
                        password: this.passwordForm.old,
                        new_password: this.passwordForm.new,
                    }).then((data) => {
                        if (data.code > -1) {
                            this.passwordForm = {
                                old: '',
                                new: ''
                            }
                            this.$Notice.success({
                                title: '修改成功!',
                            });

                        } else {
                            this.errorHandle(data.msg)
                        }
                    }, () => {
                        this.errorHandle()
                    }).then(() => {
                        this.pswIsLoading = false;
                    })
                }
            })
        },
        beforeUpload(file) {
            this.uploadProgress = 0;
            this.isUploading = true;
            return true
        },
        uploadSuccess(data) {
            this.isUploading = false;

            if (data.code > -1) {
                this.$nextTick(() => {
                    this.user.bigicon = data.data.bigicon;
                })
                this.$store.commit('updateUser', this.user, true)
            } else {
                if (data.code == -2) {
                    this.$store.commit("logout");
                    this.$store.commit("clearAllTags");
                    this.$store.commit("clearOpenedSubmenu");
                    this.$Notice.error({
                        title: '提示',
                        desc: '长时间未操作，请重新登录'
                    });
                    this.$router.replace({
                        name: 'login'
                    })
                } else {
                    this.errorHandle(data.msg)
                }
            }
        },
        uploadError(e) {
            console.log('error', e);
            this.isUploading = false;
            this.errorHandle()
        },
        uploadProgressFunc(event, file, fileList) {
            this.uploadProgress = Math.round(event.percent)
        },
        handleFormatError() {
            this.isUploading = false;
            this.errorHandle('请选择正确的图片！')

        },
        handleMaxSize() {
            this.isUploading = false;
            this.errorHandle('图片超出大小限制！')
        }
    },
    mounted() {

        setTimeout(() => {
            this.imgUploadPath = this.postUrl.uploadUserHeader;
            let body = {
                token: this.user.tokenList[0],
                app_key: this.http.app_key,
                timestamp: '2017-09-26 15:52:28'
            }
            let sign = this.http.getSign({
                token: this.user.tokenList[0],
            })
            body.sign = sign;
            console.log(body);
            this.uploadImgdata = body
            this.personalInfoForm = {
                name: this.user.name,
                password: '',
                mail: this.user.email,
                tel: this.user.tel
            }
        }, 200);
    }
}
